package com.byxy.action;

import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;

import com.byxy.entity.Forder;
import com.byxy.entity.Product;
import com.byxy.entity.Sorder;
import com.byxy.entity.User;
import com.byxy.service.ForderService;
import com.byxy.service.ProductService;
import com.byxy.service.SorderService;
import com.opensymphony.xwork2.ActionSupport;

public class DetailAction extends ActionSupport {

	private String id;
	private Product product;
	@Resource
	private ProductService productService;
	@Resource
	private SorderService sorderService;

	@Resource
	private ForderService forderService;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String showDetail() {
		System.out.println("Id = " + id);
		product = productService.get(id);

		return "success";
	}

	public Product getProduct() {
		return product;
	}

	public void setProduct(Product product) {
		this.product = product;
	}

	public String addSorder() {
		product = productService.get(product.getId().toString());
		Sorder sorder = new Sorder();
		sorder.setName(product.getName());
		sorder.setNumber(1);
		sorder.setPrice(product.getPrice());
		sorder.setProduct(product);
		User u = (User) ServletActionContext.getRequest().getSession().getAttribute("u");
		List<Forder> forders = forderService.getByUser(u);

		for (Forder f : forders) {
			sorder.setForder(f);
			break;
		}

		sorderService.add(sorder);
		return "success";
	}
}
